package runze.util.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FileUtils;

/**
 * @author wrzhxy@qq.com
 * @创建时间 2017年5月24日 下午5:49:52
 */
public class UploadUtil {
	static public String uploadPic(File pic, String picname, String path) {
		String filename = null;
		
		if (pic != null || picname != null) {
			// 把文件上传到upload目录
			// 1. 获取上传的目录路径
			// 2. 创建目标文件对象
			String uuid = UUID.randomUUID().toString();
			// String[] name_list = thumbnailFileName.split("."); 获取后缀名
			String suffix = picname.substring(picname.indexOf('.'), picname.length());
			
			filename = uuid.concat(suffix);
			File destFile = new File(path, filename);
			// 3. 把上传的文件，拷贝到目标文件中
			try {
				FileUtils.copyFile(pic, destFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 把不带路径的文件名返回
		return filename;
	}
	static public String uploadThumb(File pic, String picname, String path) {
		picname = "th_" + picname;
		if (pic != null || picname != null) {
			// 把文件上传到upload目录
			// 1. 获取上传的目录路径
			// 2. 创建目标文件对象
			
			File destFile = new File(path, picname);
			// 3. 把上传的文件，拷贝到目标文件中
			try {
				FileUtils.copyFile(pic, destFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 把不带路径的文件名返回
		return picname;
	}
	static public String updatePic(File pic, String path, String picname) {
		
		if (pic != null || picname != null) {
			// 把文件上传到upload目录
			// 1. 获取上传的目录路径
			// 2. 创建目标文件对象
			File destFile = new File(path + "/" + picname);
			// 3. 把上传的文件，拷贝到目标文件中
			try {
				// The directory holding the destination file is created if it does not exist. 
				// If the destination file exists, then this method will overwrite it.
				FileUtils.copyFile(pic, destFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return picname;
	}
	static public void removePic (String path, String picname) {
		FileUtils.deleteQuietly(new File(path + "/" + picname));	// 这是删除目录的
//		try {
//			FileUtils.deleteDirectory(new File(picname));	// 这是删除目录的
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	static public String uploadHtml(String serverPath, StringBuilder sb) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddmmssSSS");
		String format = sdf.format(new Date());
		format += (int) (10 * Math.random());
		format += (int) (10 * Math.random());
		format += (int) (10 * Math.random());
		
		String filename = format + ".html";
        // 设置上传路径
        File file = new File(serverPath, filename);
        BufferedWriter writer = null;
        
		try {
			FileOutputStream writerStream = new FileOutputStream(file);
			
			writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8")); 
			
			writer.write(sb.toString());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();  
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return filename;
	}
	static public String uploadHtml_m(String serverPath, StringBuilder sb) {
		// 设置上传路径
		File file = new File(serverPath);
		
		BufferedWriter writer = null;
		try {
			FileOutputStream writerStream = new FileOutputStream(file);
			
			writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8")); 
			
			writer.write(sb.toString());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();  
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return serverPath;
	}
//		BufferedReader br = null;
//        ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes());
//        br = new BufferedReader();	//此时获取到的br就是整个文件的缓存流
//        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8")));	//确认流的输出文件和编码格式，此过程创建了“test.txt”实例
//        while ((str = br.readLine())!= null) // 判断最后一行不存在，为空结束循环
//        {
//        	bw.write(str);
//        };
//        bw.close();//关闭流
//        bre.close();//关闭流
	
//        ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes());
//        OutputStream os = null;
//		try {
//			os = new FileOutputStream(file);
//			int bytesRead = 0;
//			byte[] buffer = new byte[8192];
//			while ((bytesRead = bais.read(buffer, 0, 8192)) != -1) {
//				os.write(buffer, 0, bytesRead);
//			}
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			if (os != null) {
//				try {
//					os.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//	        try {
//				bais.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
}
